package chapter17;

/**
 * @Auther: Tx
 * @Date: 2022/7/13 10:33
 */

//守护线程
//设置为守护线程后，当被守护的线程执行完毕后，守护线程自动结束
public class DaemonThread {
    public static void main(String[] args) {
        MyDaemonThread myDaemonThread = new MyDaemonThread();
        Thread thread = new Thread(myDaemonThread);
        thread.setDaemon(true);
        thread.start();
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("主线程运行");
        }
    }
}

class MyDaemonThread implements Runnable {


    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("守护线程运行");
        }
    }
}
